﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HomeWork3
{
    public abstract class Shape {
        abstract public double GetArea();
    }
    public class Square : Shape
    {
        public double area;
        public Square(double l) {
            if (l > 0)
                this.area = l * l;
            else
                this.area = -1;
        }
        override public double GetArea()
        {
            return area;
        }
    }
    public class Rectangle : Shape
    {
        public double area;
        public Rectangle(double w, double h) {
            if (w > 0 && h > 0)
                this.area = w * h;
            else
                this.area = -1;
        }
        override public double GetArea()
        {
            return area;
        }
    }

    public class Triangle : Shape
    {
        public double area;
        public Triangle(double a, double b, double c) {
            double p = (a + b + c) / 2;
            if (a > 0 && b > 0 && c > 0 && p - a > 0 && p - b > 0 && p - c > 0)
                this.area = Math.Sqrt(p * (p - a) * (p - b) * (p - c));
            else
                this.area = -1;
        }
        override public double GetArea()
        {
            return area;
        }
    }
    
    public class ShapeFactory
    {
        public enum ShapeType {Square=0,Rectangle,Triangle}
        public Shape Create()
        {
            Random r = new Random(Guid.NewGuid().GetHashCode());
            int type = r.Next()%3;
            Shape shape;
            ShapeType shapeType=(ShapeType)type;
            switch (shapeType)
            {
                case ShapeType.Square:
                    Console.WriteLine("创建正方形");
                   shape= new Square(r.NextDouble()*10);
                    break;
                case ShapeType.Rectangle:
                    Console.WriteLine("创建长方形");
                    shape = new Rectangle(r.NextDouble() * 10, r.NextDouble() * 10);
                    break;
                case ShapeType.Triangle:
                    Console.WriteLine("创建三角形");
                    shape = new Triangle(r.NextDouble() * 10, r.NextDouble() * 10, r.NextDouble() * 10);
                    break;
                default:
                    shape = null;
                    break;
            }
            return shape;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            double sum=0;
            int shapeNumber = 0;
            ShapeFactory factory = new ShapeFactory();
            while (shapeNumber < 10)
            {
                
                double area = factory.Create().GetArea();
                if ( area>0)
                {
                    Console.WriteLine($"第{shapeNumber+1}个图形的面积是 " + area);
                    shapeNumber++;
                    sum +=area;
                }
                else
                    Console.WriteLine("该图形不合法 ");
            }
            Console.WriteLine("总面积为 " + sum);
        }
    }
}
